{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, BatchNormalization, Add\n",
    "from keras import backend as K\n",
    "from collections import OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=6):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "DATA = OrderedDict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### graph 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "random_seed = 10001\n",
    "data_in_shape = (8, 8, 2)\n",
    "\n",
    "input_layer_0 = Input(shape=data_in_shape)\n",
    "branch_0 = Conv2D(4, (3,3), activation='relu', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True)(input_layer_0)\n",
    "\n",
    "input_layer_1 = Input(shape=data_in_shape)\n",
    "branch_1 = Conv2D(4, (3,3), activation='relu', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True)(input_layer_1)\n",
    "\n",
    "output_layer = Add()([branch_0, branch_1])\n",
    "model = Model(inputs=[input_layer_0, input_layer_1], outputs=output_layer)\n",
    "\n",
    "data_in = []\n",
    "for i in range(2):\n",
    "    np.random.seed(random_seed + i)\n",
    "    data_in.append(np.expand_dims(2 * np.random.random(data_in_shape) - 1, axis=0))\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(random_seed + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(data_in)\n",
    "data_out_shape = result[0].shape\n",
    "data_in_formatted = [format_decimal(data_in[i].ravel().tolist()) for i in range(2)]\n",
    "data_out_formatted = format_decimal(result[0].ravel().tolist())\n",
    "\n",
    "DATA['graph_01'] = {\n",
    "    'inputs': [{'data': data_in_formatted[i], 'shape': data_in_shape} for i in range(2)],\n",
    "    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],\n",
    "    'expected': {'data': data_out_formatted, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### export for Keras.js tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "filename = '../../test/data/graph/01.json'\n",
    "if not os.path.exists(os.path.dirname(filename)):\n",
    "    os.makedirs(os.path.dirname(filename))\n",
    "with open(filename, 'w') as f:\n",
    "    json.dump(DATA, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"graph_01\": {\"inputs\": [{\"data\": [0.614539, -0.766986, 0.408571, 0.047078, -0.6846, 0.784464, 0.388013, 0.800908, 0.168812, -0.389806, -0.317231, -0.455732, 0.005666, -0.753868, 0.656084, 0.433935, -0.129529, 0.017374, -0.074927, -0.853786, 0.046388, -0.110576, 0.585628, 0.932795, 0.616368, 0.041046, -0.725788, -0.466342, -0.866621, -0.401956, -0.83338, -0.006886, -0.895257, -0.871749, -0.763091, 0.195421, 0.652286, -0.133466, 0.539261, -0.252054, -0.385686, -0.272843, -0.743031, -0.914618, 0.082555, -0.542999, 0.850809, 0.590702, 0.471015, 0.878308, -0.006015, -0.317852, 0.969617, -0.003114, -0.691904, -0.646996, 0.178168, 0.665631, -0.506366, 0.985707, -0.081918, -0.91072, -0.587681, 0.251821, -0.28637, -0.088609, -0.543521, -0.786418, 0.566196, 0.856439, -0.616265, 0.837125, 0.017606, 0.87061, 0.435946, -0.292005, 0.24717, 0.052038, -0.083473, 0.133176, -0.931298, -0.49755, 0.290547, -0.522794, 0.513177, 0.860086, 0.899028, 0.595195, 0.42278, 0.22579, 0.839063, 0.254697, -0.376358, 0.651431, 0.071884, 0.434822, -0.240557, 0.229485, 0.474922, 0.243518, -0.370605, -0.125617, -0.709538, -0.470422, -0.478649, -0.179988, -0.998803, -0.270052, -0.643717, -0.504661, -0.0284, -0.200256, 0.035329, 0.457065, 0.345434, -0.6177, 0.136493, 0.478197, 0.268122, 0.876629, -0.873231, -0.440935, -0.731665, -0.993905, -0.819497, 0.115028, -0.291797, 0.580689], \"shape\": [8, 8, 2]}, {\"data\": [-0.096224, 0.673607, -0.009657, 0.52201, -0.470713, 0.710414, -0.62915, 0.836049, 0.958218, 0.675545, 0.87071, -0.765923, 0.270454, -0.605797, -0.781502, -0.158478, 0.942449, -0.098396, 0.479279, -0.663835, -0.216723, -0.551944, 0.672006, 0.615437, 0.936846, 0.765668, -0.725233, -0.59916, -0.233731, 0.031473, 0.927414, 0.759562, 0.97423, 0.419831, 0.318504, -0.031154, 0.959492, -0.136413, 0.062065, -0.31904, -0.460297, -0.691724, 0.576339, 0.606164, 0.677099, -0.821884, 0.706775, 0.598279, -0.373949, -0.663068, 0.974704, -0.157164, -0.934793, 0.745087, -0.871081, -0.580079, -0.015164, -0.319471, -0.336323, 0.227711, 0.345044, 0.021435, 0.742563, 0.859598, -0.887057, -0.354838, 0.668705, -0.308794, 0.971958, -0.477421, 0.436958, 0.606519, -0.24108, 0.81307, -0.945765, -0.34327, 0.715052, -0.497423, 0.816045, 0.822065, 0.506868, -0.851311, 0.738795, 0.67809, -0.644936, -0.587803, -0.59148, -0.156544, 0.353301, 0.907141, -0.404002, 0.865169, 0.93593, -0.265458, -0.604581, -0.908579, -0.59238, -0.2719, -0.254299, -0.628083, -0.39186, 0.647615, 0.880208, 0.326923, 0.393419, 0.45855, -0.765769, -0.658543, 0.884576, 0.299188, -0.826467, -0.342861, 0.566709, -0.196027, -0.030079, 0.274921, -0.332923, 0.012984, 0.554705, 0.975116, 0.010699, -0.178185, -0.972592, -0.482001, -0.31068, 0.678962, 0.670572, 0.120115], \"shape\": [8, 8, 2]}], \"weights\": [{\"data\": [0.614539, -0.766986, 0.408571, 0.047078, -0.6846, 0.784464, 0.388013, 0.800908, 0.168812, -0.389806, -0.317231, -0.455732, 0.005666, -0.753868, 0.656084, 0.433935, -0.129529, 0.017374, -0.074927, -0.853786, 0.046388, -0.110576, 0.585628, 0.932795, 0.616368, 0.041046, -0.725788, -0.466342, -0.866621, -0.401956, -0.83338, -0.006886, -0.895257, -0.871749, -0.763091, 0.195421, 0.652286, -0.133466, 0.539261, -0.252054, -0.385686, -0.272843, -0.743031, -0.914618, 0.082555, -0.542999, 0.850809, 0.590702, 0.471015, 0.878308, -0.006015, -0.317852, 0.969617, -0.003114, -0.691904, -0.646996, 0.178168, 0.665631, -0.506366, 0.985707, -0.081918, -0.91072, -0.587681, 0.251821, -0.28637, -0.088609, -0.543521, -0.786418, 0.566196, 0.856439, -0.616265, 0.837125], \"shape\": [3, 3, 2, 4]}, {\"data\": [-0.096224, 0.673607, -0.009657, 0.52201], \"shape\": [4]}, {\"data\": [0.841248, 0.161158, 0.958983, 0.490417, 0.824656, 0.052908, 0.775934, 0.147982, -0.867877, -0.555668, -0.331903, 0.40502, -0.595569, -0.471818, -0.559532, -0.57215, -0.89974, -0.573032, -0.35398, -0.705059, -0.927461, -0.650224, -0.563291, -0.79357, 0.061125, 0.136481, -0.97837, 0.072996, -0.878323, 0.62697, -0.667599, -0.553128, 0.009751, -0.264958, -0.054374, -0.140509, 0.513801, 0.652987, 0.027122, 0.024481, -0.967907, 0.249529, 0.127448, -0.037277, -0.270137, 0.320024, -0.432672, 0.754105, -0.324482, 0.154028, -0.336313, -0.608767, 0.80611, -0.203209, -0.131729, 0.218894, -0.561637, -0.664737, 0.711306, 0.08824, -0.771977, 0.860109, 0.782578, -0.50916, -0.445579, -0.333019, 0.295793, 0.565551, -0.946729, -0.858205, -0.673907, -0.061506], \"shape\": [3, 3, 2, 4]}, {\"data\": [-0.592446, -0.52773, 0.08531, -0.949905], \"shape\": [4]}], \"expected\": {\"data\": [0.0, 0.0, 0.511377, 0.761137, 0.089673, 0.0, 2.344596, 1.83067, 0.0, 1.288275, 0.209278, 0.52201, 0.0, 0.583482, 0.0, 0.361793, 0.338753, 2.302857, 3.135058, 0.769982, 0.0, 2.61706, 2.678339, 3.256685, 1.354372, 2.927612, 0.44725, 0.0, 0.0, 0.936231, 0.0, 1.174226, 0.740215, 1.057914, 0.0, 0.0, 0.458034, 1.715151, 0.887505, 4.730055, 2.323795, 0.643991, 3.631715, 0.0, 0.451368, 1.871133, 0.460517, 1.002624, 0.654007, 1.090218, 2.492313, 1.221591, 0.0, 1.012071, 0.0, 2.678558, 2.713511, 0.85377, 2.097393, 0.0, 1.328144, 0.038101, 2.073901, 0.727267, 1.185441, 2.954031, 0.0, 0.0, 0.0, 1.274613, 0.807891, 1.763073, 0.0, 1.787846, 2.005306, 1.585001, 3.587808, 0.527242, 0.216876, 1.782509, 2.980172, 0.933003, 0.0, 1.429616, 0.282556, 0.633336, 0.0, 0.381211, 0.0, 1.757307, 0.321523, 2.343741, 0.111566, 2.637389, 0.829683, 1.324382, 0.0, 0.722417, 0.629871, 1.96819, 1.104287, 0.0, 1.282939, 0.026865, 0.0, 0.0, 2.556382, 2.134724, 2.26546, 1.608411, 1.964203, 0.446187, 0.052762, 0.873849, 1.30612, 0.662152, 0.586174, 0.0, 1.183158, 0.0, 0.014308, 2.0325, 0.195094, 0.662305, 2.315926, 0.81671, 2.092357, 2.257756, 0.62406, 0.371729, 1.367249, 1.868175, 1.22012, 0.38947, 3.479828, 0.0, 0.428697, 1.161613, 3.363639, 2.099957, 1.265016, 0.0, 6.031105, 3.860233], \"shape\": [6, 6, 4]}}}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(DATA))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
